//
//  ZYPoperAnimator.swift
//  SwiftDemo1
//
//  Created by wangzhipan on 2025/6/5.
//

import Foundation
import UIKit

class ZYPoperAnimator: NSObject {
    private var isPresented: Bool = false
    var callback: ((_ isPresented: Bool)-> Void)?
    var presentedViewFrame: CGRect = CGRect.zero
    override init() {}
    
    init(callback: ((_: Bool) -> Void)? = nil) {
        self.callback = callback
    }
}


extension ZYPoperAnimator: UIViewControllerTransitioningDelegate {
    //重写UIPresentationController的willLayout方法，在这里设置presentedVc大小
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let presnetController = ZYPresentationController(presentedViewController: presented, presenting: presenting)
        presnetController.presentedViewFrame = presentedViewFrame
        return presnetController
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> (any UIViewControllerAnimatedTransitioning)? {
        isPresented = true
        callback?(isPresented)
        return self
    }

    func animationController(forDismissed dismissed: UIViewController) -> (any UIViewControllerAnimatedTransitioning)? {
        isPresented = false
        callback?(isPresented)
        return self
    }
}

extension ZYPoperAnimator: UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: (any UIViewControllerContextTransitioning)?) -> TimeInterval {
        return 0.3
    }

    func animateTransition(using transitionContext: any UIViewControllerContextTransitioning) {
        isPresented ? animationForPresneted(transitionContext: transitionContext) : animationForDismiss(transitionContext: transitionContext)
    }
    
    private func animationForPresneted(transitionContext: any UIViewControllerContextTransitioning) {
        //获取弹出的presentedView
        if let presentedView = transitionContext.view(forKey: .to) {
            transitionContext.containerView.addSubview(presentedView)
            presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            presentedView.transform = CGAffineTransformMakeScale(1, 0)
            UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
                presentedView.transform = CGAffineTransform.identity
            } completion: { _ in
                transitionContext.completeTransition(true)
            }

        }
    }
    
    private func animationForDismiss(transitionContext: any UIViewControllerContextTransitioning) {
        //获取dismissView
        if let dismissView = transitionContext.view(forKey: .from) {
            UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
                dismissView.transform = CGAffineTransformMakeScale(1, 0.001)
            } completion: { _ in
                dismissView.removeFromSuperview()
                transitionContext.completeTransition(true)
            }

        }
    }
}

